package com.dq.eduservice.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dq.commonutils.pojo.DqException;
import com.dq.eduservice.entity.EduSubject;
import com.dq.eduservice.entity.excel.SubjectData;
import com.dq.eduservice.service.EduSubjectService;

import java.util.Map;

/**
 * @project: guli_parent
 * @ClassName: SubjectListener
 * @author: dq
 * @creat: 2022/7/21 16:03
 */
public class SubjectListener extends AnalysisEventListener<SubjectData> {
    /**
     * 1. 出现一个问题  此监听器不是Spring管理
     * 2. 无法注入service层
     */
    public EduSubjectService subjectService;

    //无参构造
    public SubjectListener() {
    }

    //有参构造
    public SubjectListener(EduSubjectService subjectService) {
        this.subjectService = subjectService;
    }

    /**
     * 获取标题
     *
     * @param headMap
     * @param context
     */
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {

    }

    /**
     * 获取内容
     * 1. 一行一行的for循环读取
     *
     * @param subjectData
     * @param analysisContext
     */
    @Override
    public void invoke(SubjectData subjectData, AnalysisContext analysisContext) {
        if (subjectData == null) {
            throw new DqException(20001, "文件数据为空");
        }

        //一行一行读取的   先一级分类   再二级分类
        /**
         *  eg: （前端     Vue学习）
         * 1. 先判断一级是否重复
         *  (1. 如果查出来没有则进行添加 如果有则不进行添加
         * 2. 再判断二级分类
         * （1. 先获取左侧一级分类的id
         *  (2. 在根据这个id查询在此一级分类下有无一样的
         *  (3. 如果有一样的则不用添加 如果无一样的就进行添加
         */
        //1级分类
        EduSubject exitOneSubject = this.exitOneSubject(subjectService, subjectData.getOneSubjectName());
        if (exitOneSubject==null){ //没有相同的一级分类 则添加  设置parentId  设置title
            exitOneSubject = new EduSubject();
            exitOneSubject.setParentId("0");
            exitOneSubject.setTitle(subjectData.getOneSubjectName());
            subjectService.save(exitOneSubject);
        }


        //如何获取parent_id？  利用上面一级判断 如果没有就插入一个（前端）然后自动填充id   如果有则直接获取到id
        String parentId = exitOneSubject.getId();

        //2级分类
        EduSubject exitTwoSubject = this.exitTwoSubject(subjectService, subjectData.getTwoSubjectName(), parentId);
        if (exitTwoSubject==null){ //没有相同的一级分类 则添加  设置parentId  设置title
            exitTwoSubject = new EduSubject();
            exitTwoSubject.setParentId(parentId);
            exitTwoSubject.setTitle(subjectData.getTwoSubjectName());
            subjectService.save(exitTwoSubject);
        }
    }

    /**
     * 判断一级分类
     * @param subjectService
     * @param name
     * @return
     */
    private EduSubject exitOneSubject(EduSubjectService subjectService,String name){
        LambdaQueryWrapper<EduSubject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduSubject::getParentId,'0').eq(EduSubject::getTitle,name).last("limit 1");
        EduSubject oneSubject = subjectService.getOne(queryWrapper);
        return oneSubject;
    }

    /**
     * 判断二级分类
     * @param subjectService
     * @param name
     * @param parentId
     * @return
     */
    private EduSubject exitTwoSubject(EduSubjectService subjectService,String name,String parentId){
        LambdaQueryWrapper<EduSubject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduSubject::getParentId,parentId).eq(EduSubject::getTitle,name).last("limit 1");
        EduSubject twoSubject = subjectService.getOne(queryWrapper);
        return twoSubject;
    }

    /**
     * 后面的操作
     *
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
